package com.developer.weatherdata.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.developer.weatherdata.R;
import com.developer.weatherdata.util.LocationListenerAdapter;
import com.developer.weatherdata.util.Util;
import com.developer.weatherdata.util.WeatherDownloader;

public class MainActivity extends Activity {

	private static final String RESTFUL_ADDRES = "https://api.forecast.io/forecast";
	private static final String API_KEY = "fb6f69352b7103bbd2ce72f372a00723";
	private static String LATITUDE = "41.00";
	private static String LONGITUDE = "29.00";
	private static final String UNIT_CELCIUS = "units=ca";
	private static String API_ADDRESS = String.format("%s/%s/%s,%s?%s", RESTFUL_ADDRES, API_KEY, LATITUDE, LONGITUDE, UNIT_CELCIUS);

	public ImageView iwRefresh;
	public TextView txtLocation;
	public ImageView iwWeatherIcon;
	public TextView txtTime;
	public TextView txtTemperature;
	public TextView txtHumidity;
	public TextView txtPrepChance;
	public TextView txtSummary;
	public ProgressBar pbRefreshing;
	public RelativeLayout rlContainer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initializeComponents();
		registerEvents();

		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		String provider = locationManager.getBestProvider(criteria, false);
		locationManager.requestLocationUpdates(provider, 400, 1, new LocationListenerAdapter() {
			@Override
			public void onLocationChanged(Location location) {
				LATITUDE = String.valueOf(location.getLatitude());
				LONGITUDE = String.valueOf(location.getLongitude());
				API_ADDRESS = String.format("%s/%s/%s,%s?%s", RESTFUL_ADDRES, API_KEY, LATITUDE, LONGITUDE, UNIT_CELCIUS);
				updateTemperature();
			}
		});

		if (Util.getInstance(this).isInternetAvailable()) {
			updateTemperature();
		} else {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Ooppps!");
			builder.setMessage("No internet connection available.");
			builder.setPositiveButton(android.R.string.ok, null);
			AlertDialog alertDialog = builder.create();
			alertDialog.show();
		}

	}

	private void initializeComponents() {
		iwRefresh = (ImageView) findViewById(R.id.iw_refresh);
		txtLocation = (TextView) findViewById(R.id.txt_location);
		iwWeatherIcon = (ImageView) findViewById(R.id.iw_weather_icon);
		txtTime = (TextView) findViewById(R.id.txt_time);
		txtTemperature = (TextView) findViewById(R.id.txt_temperature);
		txtHumidity = (TextView) findViewById(R.id.txt_humidity);
		txtPrepChance = (TextView) findViewById(R.id.txt_prep_chance);
		txtSummary = (TextView) findViewById(R.id.txt_summary);
		pbRefreshing = (ProgressBar) findViewById(R.id.pb_refreshing);
		rlContainer = (RelativeLayout) findViewById(R.id.rl_container);
	}

	private void registerEvents() {
		iwRefresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				updateTemperature();
			}
		});
	}

	private void updateTemperature() {
		WeatherDownloader weatherDownloader = new WeatherDownloader(this);
		weatherDownloader.execute(API_ADDRESS);
	}

}
